#include "operators.h"
#include <service.h>
#include <QDebug>
namespace Ope {
Operators::Operators()
{

}

bool Operators::Threshold(QString str)
{
    qDebug()<<"二值化"<<str;
    HObject image,region;
    QString image_Nmae=Ser::Service::Instance()->Extract_variable(str.section("#",1,1));
    QString region_Name=Ser::Service::Instance()->Extract_variable(str.section("#",2,2));
    int min=str.section("#",3,3).toInt();
    int max=str.section("#",4,4).toInt();
    bool isNull=Ser::Service::Instance()->find_HObject(image_Nmae,&image);
    if(!isNull){
        return false;
    }
    try{
         HalconCpp::Threshold(image,&region,min,max);
    }catch(HException e){
        return false;
    }

    Ser::Service::Instance()->image_Map.insert(region_Name,region);
    return true;
}

bool Operators::Connection(QString str)
{
    qDebug()<<str;
    HObject image,region;
    QString image_Nmae=Ser::Service::Instance()->Extract_variable(str.section("#",1,1));
    QString region_Name=Ser::Service::Instance()->Extract_variable(str.section("#",2,2));
    bool isNull=Ser::Service::Instance()->find_HObject(image_Nmae,&image);
    if(!isNull){
        return false;
    }
    try{
     HalconCpp::Connection(image,&region);
    }catch(HException e){
        return false;
    }
    HalconCpp::HTuple x;
    HalconCpp::CountObj(region,&x);
    qDebug()<<"区域个数:"<<x.I();
    Ser::Service::Instance()->image_Map.insert(region_Name,region);
    return true;
}

bool Operators::Opening_Circle(QString str)
{
    qDebug()<<str;
    HObject image,region;
    QString image_Nmae=Ser::Service::Instance()->Extract_variable(str.section("#",1,1));
    QString region_Name=Ser::Service::Instance()->Extract_variable(str.section("#",2,2));
    double radius=str.section("#",3,3).toDouble();
    bool isNull=Ser::Service::Instance()->find_HObject(image_Nmae,&image);
    if(!isNull){
        return false;
    }
    try{
        HalconCpp::OpeningCircle(image,&region,radius);
    }catch(HException e){
        return false;
    }

    Ser::Service::Instance()->image_Map.insert(region_Name,region);
    return true;
}

bool Operators::Fill_Up(QString str)
{
    qDebug()<<str;
    HObject image,region;
    QString image_Nmae=Ser::Service::Instance()->Extract_variable(str.section("#",1,1));
    QString region_Name=Ser::Service::Instance()->Extract_variable(str.section("#",2,2));
    bool isNull=Ser::Service::Instance()->find_HObject(image_Nmae,&image);
    if(!isNull){
        return false;
    }
    try{
        HalconCpp::FillUp(image,&region);
    }catch(HException e){
        return false;
    }

    Ser::Service::Instance()->image_Map.insert(region_Name,region);
    return true;
}

bool Operators::Shape_Trans(QString str)
{
    qDebug()<<str;
    HObject image,region;
    QString image_Nmae=Ser::Service::Instance()->Extract_variable(str.section("#",1,1));
    QString region_Name=Ser::Service::Instance()->Extract_variable(str.section("#",2,2));
    //QString type=str.section("#",3,3);
    HTuple Htype="rectangle2";
    bool isNull=Ser::Service::Instance()->find_HObject(image_Nmae,&image);
    if(!isNull){
        return false;
    }
    try{
        HalconCpp::ShapeTrans(image,&region,Htype);
    }catch(HException e){
        return false;
    }

    Ser::Service::Instance()->image_Map.insert(region_Name,region);
    return true;
}

bool Operators::Reduce_Domain(QString str)
{
    qDebug()<<str;
    HObject image,region,regionReduce;
    QString image_Nmae=Ser::Service::Instance()->Extract_variable(str.section("#",1,1));
    QString region_Name=Ser::Service::Instance()->Extract_variable(str.section("#",2,2));
    QString region_Reduce=Ser::Service::Instance()->Extract_variable(str.section("#",3,3));
    HTuple Htype="rectangle2";
    bool isNull=Ser::Service::Instance()->find_HObject(image_Nmae,&image);
    if(!isNull){
        return false;
    }
    isNull=Ser::Service::Instance()->find_HObject(region_Name,&region);
    if(!isNull){
        return false;
    }
    try{
        HalconCpp::ReduceDomain(image,region,&regionReduce);
    }catch(HException e){
        return false;
    }

    Ser::Service::Instance()->image_Map.insert(region_Reduce,regionReduce);
    return true;
}

bool Operators::Select_Shape(QString str)
{
    qDebug()<<str;
    HObject image,region,regionReduce;
    QString image_Nmae=Ser::Service::Instance()->Extract_variable(str.section("#",1,1));
    QString region_Name=Ser::Service::Instance()->Extract_variable(str.section("#",2,2));
    double min=str.section("#",5,5).toDouble();
    double max=str.section("#",6,6).toDouble();
    bool isNull=Ser::Service::Instance()->find_HObject(image_Nmae,&image);
    if(!isNull){
        return false;
    }
    try{
        HalconCpp::SelectShape(image,&region,"area","and",min,max);
    }catch(HException e){
        return false;
    }

    Ser::Service::Instance()->image_Map.insert(region_Name,region);
    return true;
}

bool Operators::Smallest_Rectangle2(QString str)
{
    qDebug()<<str;
    HObject region;
    QString image_Nmae=Ser::Service::Instance()->Extract_variable(str.section("#",1,1));
    QString qrow=str.section("#",2,2);
    QString qcolumn=str.section("#",3,3);
    QString qphi=str.section("#",4,4);
    QString qlength1=str.section("#",5,5);
    QString qlength2=str.section("#",6,6);
    HTuple row=0,column=0,phi=0,length1=0,length2=0;
    bool isNull=Ser::Service::Instance()->find_HObject(image_Nmae,&region);
    HalconCpp::HTuple x;
    HalconCpp::CountChannels(region,&x);
    qDebug()<<"通道数:"<<x.I();
    if(!isNull){
        return false;
    }
    try{
        HalconCpp::SmallestRectangle2(region,&row,&column,&phi,&length1,&length2);
    }catch(HException e){
        return false;
    }
    //qDebug()<<qrow<<" "<<qcolumn<<" "<<qphi<<" "<<qlength1<<" "<<qlength2<<" ";
    //qDebug()<<row.D()<<" "<<column.D()<<" "<<phi.D()<<" "<<length1.D()<<" "<<length2.D()<<" ";
    Ser::Service::Instance()->parameter_Map.insert(qrow,row);
    Ser::Service::Instance()->parameter_Map.insert(qcolumn,column);
    Ser::Service::Instance()->parameter_Map.insert(qphi,phi);
    Ser::Service::Instance()->parameter_Map.insert(qlength1,length1);
    Ser::Service::Instance()->parameter_Map.insert(qlength2,length2);
    return true;
}

bool Operators::Gen_Rectangle2(QString str)
{
    qDebug()<<str;
    HObject region;
    QString image_Nmae=Ser::Service::Instance()->Extract_variable(str.section("#",1,1));
    QString qrow=str.section("#",2,2);
    QString qcolumn=str.section("#",3,3);
    QString qphi=str.section("#",4,4);
    QString qlength1=str.section("#",5,5);
    QString qlength2=str.section("#",6,6);
    HTuple row,column,phi,length1,length2;
    bool isNull=Ser::Service::Instance()->find_HTuple(qrow,&row);
    if(!isNull){
        return false;
    }
    isNull=Ser::Service::Instance()->find_HTuple(qcolumn,&column);
    if(!isNull){
        return false;
    }
    isNull=Ser::Service::Instance()->find_HTuple(qphi,&phi);
    if(!isNull){
        return false;
    }
    isNull=Ser::Service::Instance()->find_HTuple(qlength1,&length1);
    if(!isNull){
        return false;
    }
    isNull=Ser::Service::Instance()->find_HTuple(qlength2,&length2);
    if(!isNull){
        return false;
    }
    try{
        HalconCpp::GenRectangle2(&region,row,column,phi,length1,length2);
    }catch(HException e){
        return false;
    }
    Ser::Service::Instance()->image_Map.insert(image_Nmae,region);
    return true;
}

bool Operators::AreaCenter(QString str)
{
    qDebug()<<str;
    HObject region;
    HTuple area,row,colum;
    QString image_Nmae=Ser::Service::Instance()->Extract_variable(str.section("#",1,1));
    QString qarea=str.section("#",2,2);
    QString qrow=str.section("#",3,3);
    QString qcolum=str.section("#",4,4);
    bool isNull=Ser::Service::Instance()->find_HObject(image_Nmae,&region);
    if(!isNull){
        return false;
    }
    try{
        HalconCpp::AreaCenter(region,&area,&row,&colum);
    }catch(HException e){
        return false;
    }
    Ser::Service::Instance()->parameter_Map.insert(qarea,area);
    Ser::Service::Instance()->parameter_Map.insert(qrow,row);
    Ser::Service::Instance()->parameter_Map.insert(qcolum,colum);
    qDebug()<<"面积:"<<area.D()<<" row: "<<row.D()<<" colum:"<<colum.D();
    return true;
}


}
